package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemdictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.UserContext;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("customer")
@Controller
public class CustomerController {
    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ISystemdictionaryService systemdictionaryService;

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") CustomerQueryObject qo) {
        // 要处理营销人员的id问题
        Subject subject = SecurityUtils.getSubject();
        // 查角色的时候查的是名字,所以填客户经理
        if (!(subject.hasRole("admin")||subject.hasRole("客户经理"))){
            Employee currentUser = UserContext.getCurrentUser();
            //不是admin和客户经理,增加营销人员的id
            qo.setSellerId(currentUser.getId());
        }
        model.addAttribute("pageInfo", customerService.queryForList(qo));
        // 跳页面之前,查对应的数据字典明细
        model.addAttribute("jobs",systemdictionaryService.queryByParentSn("job"));
        model.addAttribute("sources",systemdictionaryService.queryByParentSn("source"));
        model.addAttribute("cms", systemdictionaryService.queryByParentSn("communicationMethod"));
        model.addAttribute("emps", employeeService.selectAll());
        return "customer/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Customer entity) {
        customerService.saveOrUpdate(entity);
        return new JsonResult();
    }

    @RequestMapping("updateStatus")
    public String updateStatus(Customer entity) {
        customerService.updateStatus(entity);
        return "redirect:/customer/list.do";
    }
}
